package org.traffometer.traffobase.control;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.traffometer.traffobase.control.command.Command;

/**
 * The central servlet that serves as the controller in the MVC pattern for JSP page dispatching.
 * 
 * @author Philip M. Johnson
 * @author Jitender Miglani (did minor changes)
 */
@SuppressWarnings("serial")
public class Controller extends HttpServlet {

  private String exceptionAttribute = "javax.servlet.jsp.jspException";
  private String commandParameter = "CommandName";

  /**
   * Receives all user requests and dispatches to appropriate command class for processing. The
   * command class is determined by the value of the "CommandName" parameter in the request; if none
   * is supplied, then "Clear" is assumed. The command class that is dispatched from this method
   * returns the page that should be displayed in response to the user request. The command class
   * that is dispatched from this method is responsible for updating the request object with new
   * attributes required by the JSP page.
   * 
   * @param request The servlet request object.
   * @param response The servlet response object.
   * @exception ServletException If problems occur with the request or rsponse.
   * @exception IOException If problems occur during request forwarding.
   */
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    // Every request to this servlet must include the "CommandName" parameter.
    String commandName = request.getParameter(commandParameter);
    RequestDispatcher dispatcher;

    // Start out with a cleared ErrorMessage attribute.
    request.setAttribute("errorMessage", "");

    // If no commandName param, assume just "index.jsp" URL and initialize.
    if (commandName == null) {
      commandName = "Clear";
    }

    // Now try to dispatch to the command class responsible for handling the requested Command.
    try {
      String commandClassName = "org.traffometer.traffobase.control.command." + commandName
          + "Command";
      Command command = (Command) Class.forName(commandClassName).newInstance();
      Page page = command.process(request);
      request.setAttribute("PageTitle", page.getTitle());
      dispatcher = getServletContext().getRequestDispatcher(page.getFileName());
    }
    catch (Exception e) {
      request.setAttribute(exceptionAttribute, e);
      dispatcher = getServletContext().getRequestDispatcher(Page.ERROR.getFileName());
    }

    // Now forward the updated request object on to the page that will be returned to the user.
    dispatcher.forward(request, response);
  }

  /**
   * Dispatches to doPost for processing.
   * 
   * @param request The servlet request.
   * @param response The servlet response.
   * @exception ServletException If problems during request/response processing.
   * @exception IOException If problems during the forward to the JSP page.
   */
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doPost(request, response);
  }
}
